# import keras
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense
import numpy as np
# from sklearn.model_selection import train_test_split

count =1
def generate_arrays_from_file(path):
    global count
    while 1:
        datas = np.loadtxt(path,delimiter=' ',dtype='int')
        x = datas[:,:2]
        y = datas[:,2:]
        print(x,y)
        print('count:'+str(count))
        count = count+1
        yield (x,y)

x_valid = np.array([[1,2],[2,3]])
y_valid = np.array([[0,1],[1,0]])
model = Sequential()
model.add(Dense(units=1000, activation='relu', input_dim=2))
model.add(Dense(units=2, activation='softmax'))
model.compile(loss='categorical_crossentropy',
                optimizer='sgd',
                metrics=['accuracy'])

model.fit_generator(generate_arrays_from_file('fit_generate.txt')
                    ,steps_per_epoch=10
                    ,epochs=2
                    ,max_queue_size=1
                    ,validation_data=(x_valid, y_valid)
                    ,workers=1)

# steps_per_epoch 每执行一次steps,就去执行一次生产函数generate_arrays_from_file
# max_queue_size 从生产函数中出来的数据时可以缓存在queue队列中
